package db

import (
	log "github.com/sirupsen/logrus"
	"src/db/db_modle"
)

// 获取紧急联系人信息
func GetLiaisonInfo(id uint) ([]string, error) {
	orm := GetMysqlOrm()
	defer orm.Close()
	var p []db_modle.Liaison
	phoneArr := make([]string, 0)
	result := orm.Where("urgent_id = ?", id).Find(&p)
	if e := result.Error; e != nil {
		log.Error(e.Error())
		return []string{}, e
	}
	for _, v := range p {
		phoneArr = append(phoneArr, v.Phone)
	}
	return phoneArr, nil
}

// 获取紧急联系人电话出现的次数
func GetNumOfUrgentContacts(phone string) (int64, error) {
	orm := GetMysqlOrm()
	defer orm.Close()
	// 除过当前联系人
	count := db_modle.Liaison{}
	result := orm.Where("phone = ?", phone).Group("uid").Find(&count)
	if e := result.Error; e != nil {
		log.Error(e.Error())
		return 0, e
	}
	return result.RowsAffected, nil
}
